//  
//  Sprawdzacz
//  
//  Author:
//       Grzegorz Żur <grzegorz.zur@gmail.com>
// 
//  Copyright (c) 2010-2011 Grzegorz Żur
// 
//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
// 
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
// 
//  You should have received a copy of the GNU General Public License
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
//

using System;
using System.Text;

namespace Sprawdzacz
{

	public abstract class Validator
	{

		public readonly string Name;

		public Validator (string name)
		{
			Name = name;
		}

		public abstract Validation Validate (string number);
		
		public virtual String Normalize (string text)
		{
			StringBuilder builder = new StringBuilder ();
			foreach (char c in text)
				if (char.IsLetterOrDigit (c))
					builder.Append (c);
			return builder.ToString ();
		}

		public virtual int[] Numbers (string number)
		{
			int[] numbers = new int[number.Length];
			for (int i = 0; i < numbers.Length; i++)
				numbers[i] = Number (number[i]);
			return numbers;
		}

		public virtual int Number (char c)
		{
			if (!char.IsDigit (c))
				throw new ArgumentException ("not a digit");
			
			return c - '0';
		}

		public int Sum (int[] weights, int[] numbers)
		{
			if (weights.Length != numbers.Length)
				throw new ArgumentException ("size of weights and numbers does not match");
			
			int sum = 0;
			for (int i = 0; i < weights.Length; i++)
				sum += weights[i] * numbers[i];
			return sum;
		}

		public int Modulo (int[] numbers, int divider)
		{
			int modulo = 0;
			foreach (int number in numbers)
				modulo = (10 * modulo + number) % divider;
			return modulo;
		}

	}
	
}
